package com.book.item.controller;

import com.book.item.pojo.Book;
import com.book.item.pojo.CateGory;
import com.book.item.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/auth")
public class BookController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/he")
    public String show(Model model, HttpSession httpSession) {
        model.addAttribute("name", httpSession.getAttribute("name"));
        String name = "党政精选";
        Book[] forObject = restTemplate.getForObject("http://book-item-serivce/book/selOne/" + name, Book[].class);
        List<Book> books = Arrays.asList(forObject);
        model.addAttribute("book", books);
        name = "限时抢购";
        forObject = restTemplate.getForObject("http://book-item-serivce/book/selOne/" + name, Book[].class);
        books = Arrays.asList(forObject);
        model.addAttribute("book_1", books);
        name = "新华推荐";
        forObject = restTemplate.getForObject("http://book-item-serivce/book/selOne/" + name, Book[].class);
        books = Arrays.asList(forObject);
        model.addAttribute("book_2", books);
        CateGory[] forObject1 = restTemplate.getForObject("http://book-item-serivce/book/sel/0", CateGory[].class);
        List<CateGory> cateGories = Arrays.asList(forObject1);
        model.addAttribute("one", cateGories);
        Map<Object, Object> map = new HashMap<>();
        Map<Object, Object> map1 = new HashMap<>();
        for (int i = 0; i < cateGories.size(); i++) {
            forObject1 = restTemplate.getForObject("http://book-item-serivce/book/sel/" + cateGories.get(i).getId(), CateGory[].class);
            for (int j = 0; j < forObject1.length; j++) {
                CateGory[] forObject2 = restTemplate.getForObject("http://book-item-serivce/book/sel/" + forObject1[j].getId(), CateGory[].class);
                map1.put(forObject1[j], forObject2);
            }
            map.put(cateGories.get(i), map1);
        }
        model.addAttribute("map", map);
        return "首页";
    }

    @RequestMapping("/hel")
    public String show1(Model model) {
        model.addAttribute("name", "Cuiyw");
        return "login";
    }

    @RequestMapping("/quit")
    public String show2(Model model, HttpSession session) {
        session.setAttribute("name", null);
        return "login";
    }

    @RequestMapping("/zhuce")
    public String show3() {
        return "注册";
    }

    @RequestMapping("/loginn")
    public String show(String password, String name) {
        User forObject = restTemplate.getForObject("http://book-user-serivce/user/sel?name=" + name, User.class);
        if (forObject != null) {
            return "注册";
        } else {
            String hashpw = BCrypt.hashpw(password, BCrypt.gensalt());

            Integer i = restTemplate.getForObject("http://book-user-serivce/user/ins?name=" + name + "&password=" + hashpw, Integer.class);
            if (i == 1) {
                return "login";
            } else {
                return "注册";
            }
        }

    }

    @RequestMapping("/show")
    public String show(Model model, String id) {
        Book book = restTemplate.getForObject("http://book-item-serivce/book/sell/" + id, Book.class);
        model.addAttribute("book", book);
        String name = "新华推荐";
        Book[] forObject = restTemplate.getForObject("http://book-item-serivce/book/selOne/" + name, Book[].class);
        List<Book> books = Arrays.asList(forObject);
        System.out.println(books.toString());
        model.addAttribute("books", books);
        Book[] forObject1 = restTemplate.getForObject("http://book-item-serivce/book/selll/" + book.getCategoryID(), Book[].class);
        Book[] cateGories = Arrays.copyOfRange(forObject1, 0, 6);
        model.addAttribute("bookk", cateGories);
        return "图书介绍";
    }

    @RequestMapping("/type")
    public String show4(Model model,String keyword) {
        String name = keyword;
        Book[] forObject1 = restTemplate.getForObject("http://book-item-serivce/book/selbyname/" + name, Book[].class);
        model.addAttribute("book", forObject1);
        name = "新华推荐";
        Book[] forObject = restTemplate.getForObject("http://book-item-serivce/book/selOne/" + name, Book[].class);
        List<Book> books = Arrays.asList(forObject);
        model.addAttribute("books", books);
        return "分类";
    }
    @RequestMapping("/mes")
    public String show5(HttpSession session,Model model){
      model.addAttribute("user",session.getAttribute("user"));
        return "信息";
    }
    @RequestMapping("/xia")
    public String show6(HttpSession session,Model model){
        model.addAttribute("user",session.getAttribute("user"));
        return "未评价";
    }
}
